package jp.michi.makimono;

import java.io.File;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.AdapterView;

public class BookActivity extends Activity implements OnMenuItemClickListener {
    protected PageLayout mPageLayout;
    protected FolderChooser mFolderChooser;
    protected SurfaceView mSurfaceView;
    protected SurfaceHolder mSurfaceHolder;
    protected AsyncRunner mAsyncRunner;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(Preference.getOrientation(this));
        init();
        Log.v("onCreate", String.valueOf(getIntent().getData()));
        showFolderChooser();
    }
    protected void init() {
        mPageLayout = new PageLayout();
        mFolderChooser = new FolderChooser(this);
        mSurfaceView = new SurfaceView(this);
        mAsyncRunner = new AsyncRunner();
        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mSurfaceHolder = holder;
            }
            @Override
            @SuppressWarnings("unused")
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                mSurfaceHolder = holder;
            }
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                mSurfaceHolder = holder;
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        mAsyncRunner.start();
    }
    @Override
    protected void onPause() {
        super.onPause();
        mAsyncRunner.interrupt();
    }
    protected void showFolderChooser() {
        mFolderChooser.mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                showBook(mFolderChooser.mArrayAdapter.getItem(position).file);
                return true;
            }
        });
        setContentView(mFolderChooser.mContainer);
    }
    protected void showBook(final File file) {
        setContentView(mSurfaceView);
        mPageLayout.load(file);
        //        mPageLayout.
        //        new Runnable() {
        //            @Override
        //            public void run() {
        //                mPageLayout.fitTo(mSurfaceView.getWidth(), mSurfaceView.getHeight());
        //            }
        //        }.run();
    }
    protected void withCanvas(SurfaceHolder surfaceHolder, CanvasEditor canvasEditor) {
        if (surfaceHolder != null) {
            Canvas canvas = surfaceHolder.lockCanvas();
            if (canvas != null) {
                canvasEditor.edit(canvas);
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
            Log.v("withCanvas", "surfaceHolder.lockCanvas returned null");
        } else {
            Log.v("withCanvas", "surfaceHolder was null");
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("open");
        menu.add("rotate");
        menu.add("title");
        for (int i = 0; i < menu.size(); i++) {
            menu.getItem(i).setOnMenuItemClickListener(this);
        }
        return true;
    }
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if ("open".equals(item.getTitle())) {
            showFolderChooser();
        } else if ("rotate".equals(item.getTitle())) {
            int orientation = (Preference.getOrientation(this) == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                    ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                    : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            setRequestedOrientation(orientation);
            Preference.setOrientation(this, orientation);
        } else if ("title".equals(item.getTitle())) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        return true;
    }
    @Override
    public boolean onTouchEvent(final MotionEvent event) {
        return true;
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}
